package cn.seocoo.platform.api.controller;

import cn.seocoo.platform.advertisement.domain.Advertisement;
import cn.seocoo.platform.advertisement.dto.AdvertisementDto;
import cn.seocoo.platform.advertisement.service.IAdvertisementService;
import cn.seocoo.platform.api.util.JSONResult;
import cn.seocoo.platform.api.util.PageBean;
import cn.seocoo.platform.common.core.domain.AjaxResult;
import cn.seocoo.platform.common.utils.AmountUtils;
import cn.seocoo.platform.common.utils.StringUtils;
import cn.seocoo.platform.user.domain.AppUser;
import cn.seocoo.platform.user.service.IAppUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @ClassName: ApiAdvertisementController
 * @description: 广告
 * @author: wujian
 * @Date: 2020-01-05 23:38
 **/
@RequestMapping("api/advertisement")
@RestController
public class ApiAdvertisementController {
    @Autowired
    private IAppUserService iAppUserService;

    @Autowired
    private IAdvertisementService iAdvertisementService;


    /**
     * @Author WuJian
     * @Description //发布广告
     * @Date 11:08 2020/1/7
     * @Param [advertisement]
     * @return cn.seocoo.platform.common.core.domain.AjaxResult
     **/
    @PostMapping("/saveAdvertisement")
    public AjaxResult saveAdvertisemen(@RequestBody Advertisement advertisement) {
        try {
            if (advertisement.getAppUserId() == null) {
                return AjaxResult.error("发布广告的用户不能为空");
            }
            AppUser appUser = iAppUserService.selectAppUserById(advertisement.getAppUserId());
            if (!AppUser.BUSINESSES_USER.equals(appUser.getUserType())) {
                if (StringUtils.isBlank(advertisement.getPicturePath())) {
                    return AjaxResult.error("图片不能为空");
                }
                if (advertisement.getVehicleTypeId() == null) {
                    return AjaxResult.error("车型不能为空");
                }
                if (advertisement.getCategoryId() == null) {
                    return AjaxResult.error("类别不能为空");
                }
                if (StringUtils.isBlank(advertisement.getAmountDto())) {
                    return AjaxResult.error("价格不能为空");
                }
                int row = iAdvertisementService.insertAdvertisement(advertisement);
                if (row > 0) {
                    return AjaxResult.success("发布成功");
                }
            } else {
                return AjaxResult.error("只有商家才能发布广告");
            }


        } catch (Exception e) {
            e.printStackTrace();
        }
        return AjaxResult.error(AjaxResult.ERROR_MSG);
    }


    /**
     * @Author WuJian
     * @Description //筛选页面中的推荐广告
     * @Date 11:08 2020/1/7
     * @Param []
     * @return cn.seocoo.platform.api.util.JSONResult
     **/
    @GetMapping("/getAdvertisement")
    public JSONResult getAdvertisement() {
        try {
            List<AdvertisementDto> advertisementDtoList = iAdvertisementService.getAdvertiseForApi();
            return JSONResult.ok(advertisementDtoList);
        } catch (Exception e) {
            e.printStackTrace();
            return JSONResult.errorMsg(JSONResult.ERROR_MSG);
        }
    }

    /**
     * @Author WuJian
     * @Description //我的发布广告
     * @Date 11:08 2020/1/7
     * @Param [appUserId, pageSize, pageNum]
     * @return cn.seocoo.platform.api.util.JSONResult
     **/
    @GetMapping("/getUserAdvertisement")
    public JSONResult getUserAdvertisement(Integer appUserId, Integer pageSize, Integer pageNum) {
        try {
            List<AdvertisementDto> advertisementDtoList = iAdvertisementService.getUserAdvertisement(appUserId, pageSize, pageNum);
            PageBean<AdvertisementDto> advertisementDtoPageBean = new PageBean<>(advertisementDtoList);
            List<AdvertisementDto> list = advertisementDtoPageBean.getList();
            if (list != null && list.size() > 0) {
                for (AdvertisementDto dto : list) {
                    if (dto.getAmount() != null) {
                        dto.setPrice(AmountUtils.changeF2W(dto.getAmount().toString()));
                    }
                }
            }
            return JSONResult.ok(advertisementDtoPageBean);
        } catch (Exception e) {
            e.printStackTrace();
            return JSONResult.errorMsg(JSONResult.ERROR_MSG);
        }

    }
}
